package com.seadragon.apps.doctorcard.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.seadragon.apps.doctorcard.model.Contact;
import com.seadragon.apps.doctorcard.model.DoctorCardResult;
import com.seadragon.apps.doctorcard.model.Friend;
import com.seadragon.apps.doctorcard.model.Request;
import com.seadragon.apps.doctorcard.repository.FriendRepository;
import com.seadragon.apps.doctorcard.repository.RequestRepository;
import com.seadragon.apps.doctorcard.service.FriendService;

import static com.seadragon.apps.doctorcard.model.DoctorCardConstants.*;

@Service("friendService")
public class FriendServiceImpl implements FriendService {
	@Autowired
	private FriendRepository friendRepository;
	@Autowired
	private RequestRepository requestRepository;
	
	@Override
	public Page<Friend> findAll(int offset, int pageSize) {
		return friendRepository.findAll(new PageRequest(offset, pageSize));
	}

	@Override
	public Friend findByTwoContacts(Contact sender, Contact recipient) {
		return friendRepository.findByTwoContacts(sender.getId(), recipient.getId());
	}

	@Override
	public List<Contact> findByContact(Contact contact, int offset, int pageSize) {
		Page<Friend> page = friendRepository.findBySenderOrRecipient(contact, contact, new PageRequest(offset, pageSize));
		List<Contact> contacts  = new ArrayList<Contact>();
		Iterator<Friend> iter = page.iterator();
		while(iter.hasNext()){
			Friend friend = iter.next();
			if(contact.getId() == friend.getSender().getId()){
				contacts.add(friend.getRecipient());
			}else{
				contacts.add(friend.getSender());
			}
		}
		return contacts;
	}

	@Override
	public Friend findById(long id) {
		return friendRepository.findOne(id);
	}

	@Override
	public Friend save(Friend friend) {
		friend.setDateConnected(new Date());
		return friendRepository.save(friend);
	}

	@Override
	@Transactional
	public DoctorCardResult remove(Contact contact, Contact contact2) {
		DoctorCardResult result = new DoctorCardResult();
		Friend friendSaved = friendRepository.findByTwoContacts(contact.getId(), contact2.getId());
		if(friendSaved == null){
			result.setStatus(RETURN_FAIL);
			result.addErrorCode(ERROR_CODE_NOT_YOUR_FRIEND);
		}else{
			Request request = requestRepository.findBySenderAndRecipient(friendSaved.getSender(), friendSaved.getRecipient());
			requestRepository.delete(request);
			friendRepository.delete(friendSaved);
			result.setStatus(RETURN_SUCCESS);
		}
		return result;
	}

	@Override
	public List<Contact> findByContact(Contact contact) {
		List<Friend> friends = friendRepository.findBySenderOrRecipient(contact, contact);
		List<Contact> contacts  = new ArrayList<Contact>();
		Iterator<Friend> iter = friends.iterator();
		while(iter.hasNext()){
			Friend friend = iter.next();
			if(contact.getId() == friend.getSender().getId()){
				contacts.add(friend.getRecipient());
			}else{
				contacts.add(friend.getSender());
			}
		}
		return contacts;
	}

	
	@Override
	public boolean isFriendOf(Contact one, Contact theOther){
		Friend friend = findByTwoContacts(one, theOther);
		if(friend == null){
			return false;
		}
		return true;
	}
}
